#include <string>

std::string Encode(const std::string& in)
{
    std::string ret = std::to_string(in.size());
    ret += "\n";
    ret += in;
    ret += "\n";
    return ret;
}

bool Decode(std::string& package, std::string& out)
{
    size_t pos = package.find("\n");
    if(pos == std::string::npos) return false;
    std::string len_str = package.substr(0, pos);
    size_t len = atoi(len_str.c_str());
    size_t total = len + 2 + len_str.size();
    if(package.size() < total) return false;
    out = package.substr(pos + 1, len);
    package.erase(0, total);
    return true;
}

class Request
{
public:
    Request()
    { }
    Request(int x, int y, char op) : _x(x), _y(y), _op(op)
    { }
    ~Request()
    { }
public:
    std::string Serialize()
    {
        std::string out = std::to_string(_x);
        out += " ";
        out += _op;
        out += " ";
        out += std::to_string(_y);
        return out;
    }
    bool DeSerialize(const std::string& in)
    {
        size_t left = in.find(" ");
        if(left == std::string::npos)
            return false;
        size_t right = in.find(" ", left + 1);
        if(right == std::string::npos)
            return false;
        if(left + 2 != right) return false;
        _x = atoi(in.substr(0, left).c_str());
        _y = atoi(in.substr(right + 1).c_str());
        _op = in[left + 1];
        return true;
    }
public:
    int _x;
    int _y;
    char _op;
};


class Response
{
public:
    Response()
    { }
    Response(int ret, int err) : _ret(ret), _errno(err)
    { }
    ~Response()
    { }
public:
    std::string Serialize()
    {
        std::string out = std::to_string(_ret);
        out += " ";
        out += std::to_string(_errno);
        return out;
    }
    bool DeSerialize(const std::string& in)
    {
        size_t pos = in.find(" ");
        if(pos == std::string::npos)
            return false;
        _ret = atoi(in.substr(0, pos).c_str());
        _errno = atoi(in.substr(pos + 1).c_str());
        return true;
    }
public:
    int _ret;
    int _errno;
};
